# 第 0018 题： 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中，如下 所示：

import xml.etree.ElementTree
from xml.dom import minidom

from openpyxl import load_workbook


def city_excel(xlsx):
    wb = load_workbook(xlsx)
    ws = wb['city']

    city_dict = {}
    for i, r in enumerate(ws.rows):
        city_dict[r[0].value] = r[1].value
    return str(city_dict)


def city_xml(filepath, data):
    doc = xml.dom.minidom.Document()
    root = doc.createElement('root')
    doc.appendChild(root)
    citys = doc.createElement('citys')
    citys.appendChild(doc.createComment('城市信息'))
    citys.appendChild(doc.createTextNode(data))
    root.appendChild(citys)
    file = open(filepath, 'w', encoding="utf-8")
    doc.writexml(file, indent='\t', addindent='\t', newl='\n', encoding="utf-8")


if __name__ == '__main__':
    data = city_excel(r'resource/xml/city.xlsx')
    print(data)
    city_xml(r'resource/xml/city.xml', data)
